package com.three.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.three.Config;
import com.three.utils.HttpUtil;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.*;

import java.util.HashMap;

public class LoginController {

    @FXML
    public TextField idField;
    @FXML
    public PasswordField passwordField;
    private Stage stage;
    private Scene homeScene;
    private Scene registerScene;

    public void setPossibleScene(Stage stage, Scene... scenes) {
        this.stage = stage;
        this.homeScene = scenes[0];
        this.registerScene = scenes[1];
    }

    @FXML
    public void onLoginBtnClick() throws IOException {
        if (idField.getText().isEmpty() && passwordField.getText().isEmpty()) {
            return;
        }
        HashMap<String, String> data = new HashMap<>();
        data.put("id", idField.getText());
        data.put("password", passwordField.getText());
        JSONObject jsonObject = HttpUtil.post("/login", data);
        System.out.println(jsonObject.getString("msg"));
        Config.authorityLevel = jsonObject.getIntValue("data");
        System.out.println(Config.authorityLevel);
    }

    public void toRegister() {
        this.stage.setScene(this.registerScene);
    }

    public void toHome() {
        this.stage.setScene(this.homeScene);
    }
}